<?php
class form
{	function includeStyle()
	{		echo ('
    	<link rel="stylesheet" type="text/css" href="style/validator.css" />
    	<script type="text/javascript" src="scripts/form.js">
    	</script>
    	<script type="text/javascript" src="ajax/addAddressForm.js">
    	</script>
    	<script type="text/javascript" src="ajax/suggest.js">
    	</script>
    	 ');	}
	function includeScript()
	{		echo ('

    	 ');
	}
	function simpleInput($el_name, $signature, $value="")
    {
    	echo ('
    	<div class="signature">'.$signature.':</div>
		<input name="'.$el_name.'" type="text" value="'.$value.'" class="simpleInput" id="'.$el_name.'Input">
		<br />
		');
	}
	function validInput($el_name, $signature, $value="")
	{
		echo ('
		<div class="signature">'.$signature.':</div>
		<input name="'.$el_name.'" type="text" value="'.$value.'" class="input"
		onblur="return validate_input ('.$el_name.', document.getElementById(\'valid'.$el_name.'\'),
		document.getElementById(\'ok'.$el_name.'\') );
		document.addfirm.valid'.$el_name.'.style.display=\'none\';"
		onKeyUp="return validate_input ('.$el_name.', document.getElementById(\'valid'.$el_name.'\'),
		document.getElementById(\'ok'.$el_name.'\'));">
		&nbsp;<img src="main_pics/nok.jpg" id="valid'.$el_name.'" style="display: inline; margin: -2px;
		width="15" height="15"">
		<img src="main_pics/ok.jpg" id="ok'.$el_name.'" style="display: none;" width="15" height="15">
		<br />');
	}
	function simplePasswordInput($el_name, $signature, $value="")
	{
    	echo ('
    	<div class="signature">'.$signature.':</div>
		<input name="'.$el_name.'" type="password" value="'.$value.'" class="simplePasswordInput">
		<br />
		');
	}
	function validatePasswordInput($el_name, $signature, $value="")
	{
		echo ('
		<div class="signature">'.$signature.':</div>
		<input name="'.$el_name.'" type="password" id="'.$el_name.'" value="'.$value.'" class="input"
		onblur="return validate_input ('.$el_name.', document.getElementById(\'valid'.$el_name.'\'),
		document.getElementById(\'ok'.$el_name.'\') );
		document.addfirm.valid'.$el_name.'.style.display=\'none\';"
		onKeyUp="return validate_input ('.$el_name.', document.getElementById(\'valid'.$el_name.'\'),
		document.getElementById(\'ok'.$el_name.'\'));">
		&nbsp;<img src="main_pics/nok.jpg" id="valid'.$el_name.'" style="display: inline; margin: -2px;"
		width="15" height="15">
		<img src="main_pics/ok.jpg" id="ok'.$el_name.'" style="display: none;" width="15" height="15">
		<br />');
	}

	function simpleAddressInput($number, $value="")
	{
		if($value)$value=explode(',', $value);
		$street=$value[0];
		$building=$value[1];
		$mallId=$value[2];
		$mallName=$value[3];
		$mallShort=$value[4];
		echo ('
		<script type="text/javascript" src="ajax/suggest.js">
		</script>
		<div class="addressInp">
			<div class="street">
				<div class="streetInput">
					<div class="signature">
						Введите улицу(проспект и др.):
					</div>
					<input class="streetInput" id="street'.$number.'" name="street'.$number.'" type="text" value="'.$street.'"
					onkeyup="StreetSuggest(document.getElementById(\'dun_list\').value, this.value, '.$number.')" autocomplete="off">
				</div>
				<div class="streetSuggest'.$number.'" id="streetSuggest'.$number.'">

				</div>
			</div>
			<div class="building">
				<div class="buildingInput">
					<div class="signature">
						№ дома:
					</div>
					<input class="buildingInput" id="building'.$number.'" name="building'.$number.'" type="text" value="'.$building.'"

					 onkeyup="return BuildingSuggest(document.getElementById(\'dun_list\').value, document.getElementById(\'street'.$number.'\').value,
					 this.value, \''.$number.'\');" autocomplete="off" />
				</div>
				<div class="buildingSuggest'.$number.'" id="buildingSuggest'.$number.'">

				</div>
			</div>
		');
			if($mallId)
			{
				echo ('
			<div class="mallSuggest'.$number.'" id="mallSuggest'.$number.'">
		    	'.$mallShort.' '.$mallName.'
		    	<input name="mallSuggest'.$number.'" type="checkbox" value="'.$mallId.','.$mallName.','.$mallShort.'" checked>
			</div>
			<input name="mallHidden'.$number.'" type="hidden" value="'.$mallId.','.$mallName.','.$mallShort.'">
				');
			}
			else
			{
				echo ('
				<div class="mallSuggest'.$number.'" id="mallSuggest'.$number.'">
				</div>
				');
			}
			echo ('
		</div>
		');
	}
    function validAddressInput($number, $value="", $city='document.getElementById(\'dun_list\').value')
	{
		if($value)$value=explode(',', $value);
		$street=$value[0];
		$building=$value[1];
		$mallId=$value[2];
		$mallName=$value[3];
		$mallShort=$value[4];
		echo ('
		<script type="text/javascript" src="ajax/suggest.js">
		</script>
		<div class="addressInp">
			<div class="street">
				<div class="streetInput">
					<div class="signature">
						Введите улицу(проспект и др.):
					</div>
					<input class="streetInput" id="street'.$number.'" name="street'.$number.'" type="text" value="'.$street.'"
					onkeyup="StreetSuggest('.$city.', this.value, '.$number.')" autocomplete="off">
				</div>
				<div class="streetSuggest'.$number.'" id="streetSuggest'.$number.'">

				</div>
			</div>
			<div class="building">
				<div class="buildingInput">
					<div class="signature">
						№ дома:
					</div>
					<input class="buildingInput" id="building'.$number.'" name="building'.$number.'" type="text" value="'.$building.'"

					 onkeyup="return BuildingSuggest('.$city.', document.getElementById(\'street'.$number.'\').value,
					 this.value, \''.$number.'\');" autocomplete="off"
					 onkeydown="return validate_input (\'building'.$number.'\', document.getElementById(\'validbuilding'.$number.'\'),
				document.getElementById(\'okbuilding'.$number.'\'));" />
				</div>
				<div class="buildingSuggest'.$number.'" id="buildingSuggest'.$number.'">

				</div>
			</div>
			<div class="validPic" style="display: inline;">
			&nbsp;<img src="main_pics/nok.jpg" id="validbuilding'.$number.'" style="display: inline; margin: -2px;"
			width="15" height="15">

			<img src="main_pics/ok.jpg" id="okbuilding'.$number.'" style="display: none;" width="15" height="15">
			</div>
		');
			if($mallId)
			{
				echo ('
			<div class="mallSuggest'.$number.'" id="mallSuggest'.$number.'">
		    	'.$mallShort.' '.$mallName.'
		    	<input name="mallSuggest'.$number.'" type="checkbox" value="'.$mallId.','.$mallName.','.$mallShort.'" checked>
			</div>
			<input name="mallHidden'.$number.'" type="hidden" value="'.$mallId.','.$mallName.','.$mallShort.'">
				');
			}
			else
			{
				echo ('
				<div class="mallSuggest'.$number.'" id="mallSuggest'.$number.'">
				</div>
				');
			}
			echo ('
		</div>
		');
	}


	function addAddressForm($number, $firstLink='')
	{		if($firstLink)$firstLink='id="firstLink"';		$newNumber=$number+1;		echo ('
		<p class="addAddress" '.$firstLink.' onclick="addAddressForm(\''.$newNumber.'\', \''.$button.'\'); this.style.display=\'none\';">Добавить адрес</p>
		<div class="addAddressForm" id="addAddress'.$newNumber.'">
		</div>
		');	}
	function addAddressValidForm($number, $button, $firstLink='')
	{
		if($firstLink)$firstLink='id="firstLink"';
		$newNumber=$number+1;
		echo ('
		<a href="#'.$newNumber.'" class="addAddress" '.$firstLink.' onclick="addAddressForm(\''.$newNumber.'\'); this.style.display=\'none\';">Добавить адрес</a>
		<div class="my_form" id="addAddress'.$newNumber.'">
		</div>
		');
	}
	function safetyCheckAll($post=array())
   	{
   		foreach($_POST as $post=>$vol)
   		{
   			$_POST[$post]=$this->safetyCheck($vol);
   		}
   	}
   	function safetyCheck($value)
	{		$value=addslashes($value);
		$value=@ strip_tags($value);
		$value=htmlspecialchars($value);
		return $value;
	}
	function checkInput($value, $rusVal, $valuePOSTname, $valueValue='', $max=30, $min=0, $regExp="", $substr='', $rusRegWarn)
	{		$warning=false;		$value=$this->safetyCheck($value);
		if($min>0 && $value=='')
		{			$warning='Вы не ввели '.$rusVal.'!';
			$_POST[$valuePOSTname]=$valueValue;
			return($warning);
		}
		if($regExp!='')
		{			if(!preg_match($regExp, $value))
			{				$warning=$rusVal.' недопустим'.$rusRegWarn.'!';
				$_POST[$valuePOSTname]=$valueValue;
				return($warning);
			}
		}
		elseif($substr!='')
		{			$forbiddenSymbols=explode(' ', $substr);			for($i=0; $i<strlen($value); $i++)
	    	{
	    		if(in_array($value[$i], $forbiddenSymbols))
	    		{
			    	$warning=$rusVal.' недопустим'.$rusRegWarn.'!';
					$_POST[$valuePOSTname]=$valueValue;
					return($warning);
				}
			}		}
		if(strlen($value)<$min)
		{			$warning=$rusVal.' не может быть короче '.$min.' символов!';
			$_POST[$valuePOSTname]=$valueValue;
			return($warning);
		}
		if(strlen($value)>$max)
		{
			$warning=$rusVal.' не может быть длиннее '.$max.' символов!';
			$_POST[$valuePOSTname]=$valueValue;
			return($warning);
		}
		return $warning;	}
}
?>